package com.xinyuan.trade.Fragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.xinyuan.trade.R;
import com.xinyuan.trade.Adapter.DynamicAdapter;
import com.xinyuan.trade.Entity.DynamicEntity;
import com.xinyuan.trade.Listener.RequestWebListener;
import com.xinyuan.trade.Net.NetUtils;
import com.xinyuan.trade.Net.NetUtils.NetCallBack;
import com.xinyuan.trade.Utils.Config;
import com.xinyuan.trade.Utils.Utils;
import com.xinyuan.trade.Widght.LoadingView;
import com.xinyuan.trade.Widght.XListView;
import com.xinyuan.trade.Widght.XListView.IXListViewListener;

/**
 * 公司动态列表
 * 
 * @author daiy
 * 
 */
public class DynamicListFragment extends BaseFragment {

	private Resources resources;
	private XListView listview_dynamic;
	private LoadingView layout_loading;
	private DynamicAdapter dynamicadapter;
	private int currentpage = 1;
	private int pagenum = 20;
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_dymaniclist, container, false);

		resources = getResources();

		initView(v);
		initData();

		return v;
	}

	private void initView(View v) {
		layout_loading = (LoadingView) v.findViewById(R.id.layout_loading);

		listview_dynamic = (XListView) v.findViewById(R.id.listview_dynamic);
		dynamicadapter = new DynamicAdapter(mActivity);
		listview_dynamic.setAdapter(dynamicadapter);
		listview_dynamic.setPullRefreshEnable(true);
		listview_dynamic.setPullLoadEnable(true);
		listview_dynamic.setAutoLoadEnable(true);
		listview_dynamic.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				dynamicadapter.clean();
				currentpage = 1;
				initData();
			}

			@Override
			public void onLoadMore() {
				currentpage += 1;
				initData();
			}
		});
		listview_dynamic.setRefreshTime(Utils.getNowTime());
	}

	private void initData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put("currentpage", Integer.toString(currentpage));
		params.put("pagenum", Integer.toString(pagenum));

		NetUtils.post(mActivity, Config.COMPANYBYPAGE, params, new NetCallBack() {

			@Override
			public void success(String json) {
				onLoad();
				listview_dynamic.setVisibility(View.VISIBLE);
				layout_loading.postHandle(LoadingView.success);
				layout_loading.setVisibility(View.GONE);

				DynamicEntity entity = new Gson().fromJson(json, DynamicEntity.class);

				List<DynamicEntity.Data> data = entity.getData();
				dynamicadapter.addListEntity(data);
			}

			@Override
			public void failed(String msg) {
				// System.out.println(msg);
				onLoad();
				layout_loading.postHandle(LoadingView.interfaceerror);
				layout_loading.setL(l);
			}
		});
	}

	private void onLoad() {
		listview_dynamic.stopRefresh();
		listview_dynamic.stopLoadMore();
		listview_dynamic.setRefreshTime(Utils.getNowTime());
	}
	
	RequestWebListener l = new RequestWebListener() {

		@Override
		public void requestWeb() {
			initData();
		}
	};
}
